package com.java.elasticsearch.activemq.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;

/**
 * @author Woo_home
 * @create 2020/5/22 14:09
 */

@Service
public class SpringMQ_Produce {

    @Autowired
    private JmsTemplate jmsTemplate;

    public static void main(String[] args){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

        // 相当于 new 一个 SpringMQ_Produce，但是这里用的是 Spring，所以这里就不用 new 了
        SpringMQ_Produce produce = (SpringMQ_Produce) ctx.getBean("springMQ_Produce");

        // 普通写法
        /*produce.jmsTemplate.send(new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                TextMessage textMessage = session.createTextMessage("****** Spring 整合 ActiveMQ ******");
                return textMessage;
            }
        });*/

        // lambda 写法
        produce.jmsTemplate.send((session) -> {
            TextMessage textMessage = session.createTextMessage("****** Spring 整合 ActiveMQ MyMessageListener******");
            return textMessage;
        });
    }
}
